package main

import (
	"flag"
	"fmt"
	"gopkg.in/olivere/elastic.v3"
	"learngo/crawler_distribute/config"
	"learngo/crawler_distribute/persist"
	"learngo/crawler_distribute/rpcsupport"
	"log"
)

var port = flag.Int("port", 0, "the port for me to listen on")

func main() {
	flag.Parse()
	if *port == 0 {
		fmt.Println("must specify a port")
	}
	// 出错强制退出
	log.Fatal(serveRpc(fmt.Sprintf(":%d", *port), config.ElasticIndex))
}

func serveRpc(host, index string) error {
	// 建立elasticsearch客户端
	client, err := elastic.NewClient(
		elastic.SetSniff(false))
	if err != nil {
		return err
	}
	// 建立rpc服务器
	return rpcsupport.ServeRpc(host, &persist.ItemSaverService{
		// rpc中注册该结构
		Client: client,
		Index:  index,
	})

}
